//配置路由相关的信息
import Vue from 'vue'
import VueRouter from 'vue-router'
import login from '../components/Login.vue'
import home from '../components/Home.vue'
import welcome from '../components/welcome.vue'
import users from '../components/user/users.vue'
import rights from '../components/power/Rights.vue'
import roles from '../components/power/Roles.vue'
import cate from '../components/goods/Cate.vue'
import params from '../components/goods/Params.vue'

Vue.use(VueRouter);

//获取原型对象上的push函数
const originalPush = VueRouter.prototype.push;
//修改原型对象中的push方法
VueRouter.prototype.push = function push(location) {
   return originalPush.call(this, location).catch(err => err)
}
const routes = [
  { path:'/login', component:login},
  { path:'/home', component:home,
  redirect:'/welcome',
  children:[{path:'/welcome',component:welcome},
  {path:'/users',component:users},
  {path:'/rights',component:rights},
  {path:'/roles',component:roles},
  {path:'/categories',component:cate},
  {path:'/params',component:params},


]
},
];



const router = new VueRouter({
  routes,
  mode:'history'
});

router.beforeEach((to, from, next) => {
  if(to.path === '/login') return next();
  const token = window.sessionStorage.getItem('token');
  if(!token) return next('/login');
  next();
})

export default router;

